//
//  ListViewController.m
//  SinceLe
//
//  Created by 李元喜 on 15/11/14.
//  Copyright © 2015年 李元喜. All rights reserved.
//

#import "ListViewController.h"
#import "ListTableViewCell.h"
#import "NetWorkHandle.h"
#import "Macrocs.h"
#import "MusicModel.h"
#import "MusicPlayHelper.h"
#import <UIImageView+WebCache.h>


@interface ListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tabelView;
@property (nonatomic, strong) NSMutableArray *dataArray;


@end

@implementation ListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadTableView];
    [self loadListData];
    self.tabelView.separatorStyle = UITableViewCellSeparatorStyleNone;

}


//数据解析
-(void)loadListData{
    
    NSString *Str = [NSString stringWithFormat:KlistUrl];
     _dataArray = [NSMutableArray array];
    [NetWorkHandle getDataWithURLString:Str compare:^(id object) {
       
        NSDictionary *dic = object;
        NSArray *arr = dic[@"data"][@"items"];
        for (NSDictionary *a in arr) {
            MusicModel *model = [[MusicModel alloc]init];
            [model setValuesForKeysWithDictionary:a];
            
            [_dataArray addObject:model];
        }
        NSLog(@"%lu==============",(unsigned long)_dataArray.count);
        
        [_tabelView reloadData];
    }];
    
}



-(void)loadTableView{
    
    _tabelView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tabelView.dataSource = self;
    self.tabelView.delegate = self;
    UINib *nib = [UINib nibWithNibName:@"ListTableViewCell" bundle:nil];
    [_tabelView registerNib:nib forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tabelView];
    
}

//cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 400;
    
}


//返回cell高度
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;

}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    //赋值
    MusicModel *model = _dataArray[indexPath.row];
    [cell.Listimage sd_setImageWithURL:[NSURL URLWithString:model.poster]];
    cell.LIstLabelben.text = model.remark;
    cell.ListLabelname.text = model.artist;
    cell.ListLabelTitle.text = model.name;
    cell.LIstLabelben.text = model.remark;
    
    
    [cell.playBtn addTarget:self action:@selector(playBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    cell.playBtn.tag = indexPath.row;
    
    return cell;
    
}


//传一个值给MusicPlayHelper
-(void)playBtnClicked:(UIButton *)sender{
    MusicModel* model = _dataArray[sender.tag];
    NSString* songUrl = model.playurl_low;
    
    MusicPlayHelper* musicPlay = [MusicPlayHelper shareMusicPlayHelper];
    [musicPlay preparePlayingMusicWithUrlString:songUrl];
    
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
